package top.letscode.adclicker.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import top.letscode.adclicker.store.SimpleDataStore;

public class AdslDialer {

  private static File REBOOT_SCRIPT = new File("c:/reboot.bat");

  /**
   * 执行CMD命令,并返回String字符串
   */
  public static String executeCmd(String strCmd) throws Exception {
    Process p = Runtime.getRuntime().exec("cmd /c " + strCmd);
    StringBuilder sbCmd = new StringBuilder();
    BufferedReader br = null;

    try {
      br = new BufferedReader(new InputStreamReader(p.getInputStream(), "GBK"));
      String line;
      while ((line = br.readLine()) != null) {
        sbCmd.append(line + "\n");
      }
    } catch (Exception e) {

    } finally {
      if (br != null) {
        br.close();
      }
    }

    return sbCmd.toString();
  }

  /**
   * 连接ADSL
   */
  public static void connAdsl(String adslTitle, String adslName, String adslPass) throws Exception {
    String adslCmd = "rasdial " + adslTitle + " " + adslName + " " + adslPass;
    boolean isSuccess = false;

    while (!isSuccess) {
      checkAndDisconnect(adslTitle);
      String tempCmd = executeCmd(adslCmd);
      // 判断是否连接成功
      if (tempCmd.indexOf("已连接") > 0) {
        System.out.println("已成功建立连接!");
        isSuccess = true;
      } else {
        System.err.println("建立连接失败: " + tempCmd);
        if (tempCmd.indexOf("692") > 0 && REBOOT_SCRIPT.exists()) {// 692必须重启才能解决
          executeCmd("start c:/reboot.bat");
        }
        Utils.sleepQuietly(5);
      }
    }
  }

  /**
   * 断开ADSL
   */
  public static void checkAndDisconnect(String adslTitle) throws Exception {
    String cutAdsl = "rasdial " + adslTitle + " /disconnect";
    String result = executeCmd(cutAdsl);

    if (result.indexOf("没有连接") != -1) {
      System.err.println(adslTitle + "连接不存在!");
    } else {
      System.out.println("连接已断开!");
    }
  }



  public static String getRealIp() throws Exception {
    BufferedReader reader = null;

    try {
      URL url = new URL("http://1212.ip138.com/ic.asp");
      URLConnection connection = url.openConnection();
      connection.setConnectTimeout(5000);
      connection.setReadTimeout(5000);
      reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "GBK"));
      String line = null;
      while ((line = reader.readLine()) != null) {
        if (line.contains("您的IP是")) {
          line = line.substring(line.indexOf("<center>") + 8, line.lastIndexOf("</center>"));

          return line;
        }
      }

    } finally {
      if (reader != null) {
        try {
          reader.close();
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }

    return null;
  }

  public static void connect() {
    List<String> adslList = SimpleDataStore.getFileLines(Constants.STORE_FILE_LINES, "/adsl.txt");
    String adslTitle = adslList.get(0);
    String adslName = adslList.get(1);
    String pass = adslList.get(2);

    String realIp = null;
    try {
      connAdsl(adslTitle, adslName, pass);
      realIp = getRealIp();
    } catch (Exception e) {
      try {
        connAdsl(adslTitle, adslName, pass);
      } catch (Exception e1) {

      }
    }

    System.out.println(realIp);
  }
}
